﻿

    module Utilities  

        open System
        open Microsoft.Xna.Framework
        open Microsoft.Xna.Framework.Graphics

        let mutable private pts:Vector3[] = null
        let mutable pointsOfLine:Vector3[] = null


        type Random
          with
            member this.NextFloat with get() = this.NextDouble() |> float32

        let random = new Random()

        let lerp (a:float32) (x:float32<'u>) (y:float32<'u>) = x * a + y * (1.0f - a)

        let random_lerp : float32<'u> -> float32<'u> -> float32<'u> = fun x y -> lerp (random.NextFloat) x y


        let private Interp(t:float32)(point_length:int) = 
                                                          let numSections = point_length - 3
                                                  
                                                          let mutable floorToint =      if( t * (float32)numSections < 0.0f) then
                                                                                            (t * (float32)numSections - 1.0f)
                                                                                        else
                                                                                            (t * (numSections|>float32))
                                                  
                                                          let currPt = MathHelper.Min(floorToint, (float32)numSections - 1.0f)|> int
                                                  
                                                          let u = t * (float32)numSections - (float32)currPt 
                                                  
                                                          let a =   pts.[currPt]
                                                          let b =   pts.[currPt + 1]
                                                          let c =   pts.[currPt + 2]
                                                          let d =   pts.[currPt + 3]

                                                          0.5f * ((-a + 3.0f * b - 3.0f * c + d) * (u * u * u) + (2.0f * a - 5.0f * b + 4.0f * c - d) * (u * u)+ (-a + c) * u+ 2.0f * b)



        let createCRSplineCurve(points:Vector3[])(number_total_points:int) =        pts <- points
                                                                                    pointsOfLine <- Array.create (number_total_points + 1) (Vector3.Zero)
                                                                                    let mutable prevPt = Interp(0.0f)(pts.Length)
                                                                                    pointsOfLine.[0] <- prevPt
                                                                                    
                                                                                    for i=1 to number_total_points do
                                                                                        let pm = (float32)i/(float32)number_total_points
                                                                                        let currPt = Interp(pm)(pts.Length)
                                                                                        pointsOfLine.[i]<- currPt
                                                                                        prevPt <- currPt
                                                                                    
                                                                                    pointsOfLine

                                                                               
                                                                            
        